package com.mdd.front.controller.coupon;

import com.mdd.common.core.AjaxResult;
import com.mdd.common.core.PageResult;
import com.mdd.common.utils.TimeUtil;
import com.mdd.front.LikeFrontThreadLocal;
import com.mdd.front.service.coupon.ICouponService;
import com.mdd.front.validate.CouponReceiveParam;
import com.mdd.front.validate.CouponUseParam;
import com.mdd.front.validate.PageParam;
import com.mdd.front.vo.coupon.CouponCenterVo;
import com.mdd.front.vo.coupon.CouponMineVo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("api/coupon")
public class CouponController {

    @Resource
    ICouponService iCouponService;


    /**
     * 领券中心列表
     *
     * @author panweiliang
     * @return Object
     */
    @GetMapping("/center")
    public Object center() {
        List<CouponCenterVo> list = iCouponService.center();
        return AjaxResult.success(list);
    }

    /**
     * 我的优惠券
     *
     * @author panweiliang
     * @return Object
     */
    @GetMapping("/mine")
    public Object mine(@Validated PageParam pageParam, @RequestParam(value = "status", defaultValue = "0") int status) {
        Integer userId = LikeFrontThreadLocal.getUserId();
        PageResult<CouponMineVo> list = iCouponService.mine(pageParam, userId, status);
        return AjaxResult.success(list);
    }

    /**
     * 课程可领用的优惠券
     *
     * @author panweiliang
     * @return Object
     */
    @GetMapping("/course")
    public Object getCourseCoupon(@RequestParam(value = "courseId") Integer courseId) {
        Integer userId = LikeFrontThreadLocal.getUserId();
        List<CouponCenterVo> list = iCouponService.getCourseCoupon(courseId, userId);
        return AjaxResult.success(list);
    }

    /**
     * 获取拥有的可用优惠券
     *
     * @author panweiliang
     * @return Object
     */
    @GetMapping("/course/mine/available")
    public Object getMineAvailableCourseCoupon(@RequestParam(value = "orderId") Integer orderId, @RequestParam(value = "amount") String amount) {
        Integer userId = LikeFrontThreadLocal.getUserId();
        List<CouponMineVo> list = iCouponService.getMineAvailableCourseCoupon(orderId, userId, amount, TimeUtil.timestamp());
        return AjaxResult.success(list);
    }

    /**
     * 获取拥有的可用优惠券
     *
     * @author panweiliang
     * @return Object
     */
    @GetMapping("/course/mine/unavailable")
    public Object getMineUnavailableCourseCoupon(@RequestParam(value = "orderId") Integer orderId, @RequestParam(value = "amount") String amount) {
        Integer userId = LikeFrontThreadLocal.getUserId();
        List<CouponMineVo> list = iCouponService.getMineUnavailableCourseCoupon(orderId, userId, amount, TimeUtil.timestamp());
        return AjaxResult.success(list);
    }

    /**
     * 我的优惠券不同状态个数
     *
     * @author panweiliang
     * @return Object
     */
    @GetMapping("/mine/count")
    public Object mineCouponCount() {
        Integer userId = LikeFrontThreadLocal.getUserId();
        Map<String, Object> list = iCouponService.getMyCouponCount(userId);
        return AjaxResult.success(list);
    }



    /**
     * 优惠券领用
     *
     * @author panweiliang
     * @param couponReceiveParam 参数
     * @return Object
     */
    @PostMapping("/receive")
    public Object receive(@Validated(value = CouponReceiveParam.create.class) @RequestBody CouponReceiveParam couponReceiveParam) {
        iCouponService.receive(couponReceiveParam);
        return AjaxResult.success();
    }

    /**
     * 使用优惠券
     *
     * @author panweiliang
     * @param couponUseParam 参数
     * @return Object
     */
    @PostMapping("/use")
    public Object use(@Validated(value = CouponUseParam.create.class) @RequestBody CouponUseParam couponUseParam) {
        iCouponService.use(couponUseParam);
        return AjaxResult.success();
    }

}
